package net.pinnoocle.playweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.aop.PassToken;
import net.pinnoocle.playcommon.exception.ShopException;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playcommon.response.ErrorCode;
import net.pinnoocle.playdao.entity.Master;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.web.QrCodeVo;
import net.pinnoocle.playdao.entity.web.master.*;
import net.pinnoocle.playservice.service.impl.MasterServiceImpl;
import net.pinnoocle.playservice.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author ye
 * @Description
 * @create 2021-11-03 17:16
 */

@RestController
@RequestMapping("/master")
@Api(tags = "主持人相关")
@Slf4j
public class MasterController {

    @Autowired
    MasterServiceImpl masterService;
    @Autowired
    UserServiceImpl userService;

    @GetMapping("homePage")
    @ApiOperation("主持人首页")
    public BaseResult<MasterHomePageVo> homePage(){
        return new BaseResult<MasterHomePageVo>().ok(masterService.homePage());
    }

    @GetMapping("masterList")
    @ApiOperation("主持人列表")
    @PassToken
    public BaseResult<PageModel<WebMasterListPageVo>> masterList(@RequestParam(name = "page") Integer page,
                                                                 @RequestParam(name = "pageSize") Integer pageSize){

        return new BaseResult<PageModel<WebMasterListPageVo>>().ok(masterService.webMasterList(page,pageSize));
    }

    @GetMapping("freeMasterList")
    @ApiOperation("空闲主持人列表")
    public BaseResult<List<WebMasterListPageVo>> freeMasterList(@RequestParam("scriptId") String scriptId){
        return new BaseResult<List<WebMasterListPageVo>>().ok(masterService.freeMasterList(scriptId));
    }

    @GetMapping("masterDetail")
    @ApiOperation("主持人详情")
    @PassToken
    public BaseResult<WebMasterDetailVo> masterDetail(@RequestParam("masterId") String masterId,
                                                      @RequestParam(name = "page") Integer page,
                                                      @RequestParam(name = "pageSize") Integer pageSize){
        return new BaseResult<WebMasterDetailVo>().ok(masterService.masterDetail(masterId,page,pageSize));
    }

    @PostMapping("updateMaster")
    @ApiOperation("更新主持人信息")
    public BaseResult<String> updateMaster(@Validated @RequestBody UpdateMasterInfoParams params){
        boolean result = masterService.updateMaster(params);
        if(result){
            return new BaseResult<String>().success("更新成功");
        }
        return new BaseResult<String>().error("更新失败");
    }

    @PostMapping("openScript")
    @ApiOperation("主持人开本")
    public BaseResult<String> openScript(@Validated @RequestBody OpenScriptParams params){
        boolean result = masterService.openScript(params);
        if(result){
            return new BaseResult<String>().success("开本成功");
        }
        return new BaseResult<String>().error("开本失败");
    }

    @GetMapping("myScriptList")
    @ApiOperation("主持人会的本")
    public BaseResult<PageModel<WebMasterScriptListPage>> masterScriptList(@RequestParam("page") Integer page,
                                                                           @RequestParam("pageSize") Integer pageSize){
        String masterId = userService.getMasterId();
        return new BaseResult<PageModel<WebMasterScriptListPage>>().ok(masterService.masterAbleScriptList(masterId,page,pageSize));
    }

    @GetMapping("deleteMyScript")
    @ApiOperation("删除我会的本")
    public BaseResult<String> deleteMyScript(@RequestParam("masterScriptId") String masterScriptId){
        boolean result = masterService.deleteMyScript(masterScriptId);
        if(result){
            return new BaseResult<String>().success("删除成功");
        }
        return new BaseResult<String>().error("删除失败");
    }

    @GetMapping("myGroupList")
    @ApiOperation("组局管理 dateStr：月.日，groupType：1 待开局 2 开局中 3 已结束")
    public BaseResult<List<MasterGroupListVo>> myGroupList(@RequestParam(name = "dateStr",required = false) String dateStr,
                                                           @RequestParam(name = "groupType",required = false) Integer groupType){
        return new BaseResult<List<MasterGroupListVo>>().ok(masterService.myGroupList(dateStr,groupType));
    }

    @GetMapping("masterStartGroup")
    @ApiOperation("开始组局")
    public BaseResult<String> masterStartGroup(@RequestParam("groupId") String groupId){
        String masterId = userService.getMasterId();
        boolean result = masterService.startGroup(groupId,masterId);
        if(result){
            return new BaseResult<String>().success("开局成功");
        }
        return new BaseResult<String>().error("开局失败");
    }

    @GetMapping("masterOverGroup")
    @ApiOperation("结束组局")
    public BaseResult<String> masterOverGroup(@RequestParam("groupId") String groupId){
        String masterId = userService.getMasterId();
        boolean result = masterService.overGroup(groupId,masterId);
        if(result){
            return new BaseResult<String>().success("结束组局成功");
        }
        return new BaseResult<String>().error("结束组局失败");
    }

    @GetMapping("leftSalary")
    @ApiOperation("DM剩余未结算工资")
    public BaseResult<Double> leftSalary(@RequestParam("masterId") String masterId){
        return new BaseResult<Double>().ok(masterService.leftSalary(masterId));
    }

    @GetMapping("masterFinance")
    @ApiOperation("主持人财务管理")
    public BaseResult<MasterFinanceVo> masterFinance(MasterFinanceListSearchParams params){
        return new BaseResult<MasterFinanceVo>().ok(masterService.masterFinance(params));
    }

    @GetMapping("recheckOpenScript")
    @ApiOperation("主持申请复核开本审核")
    public BaseResult<String> recheckOpenScript(@RequestParam("openId") String openId){
        boolean result = masterService.recheckOpenScript(openId);
        if(result){
            return new BaseResult<String>().success("申请复核成功");
        }
        return new BaseResult<String>().error("申请复核失败");
    }

    @GetMapping("financeDetail")
    @ApiOperation("主持人收支明细")
    public BaseResult<PageModel<FinanceDetailListPageVo>> financeDetail(MasterFinanceListSearchParams params){
        return new BaseResult<PageModel<FinanceDetailListPageVo>>().ok(masterService.financeDetail(params));
    }

    @GetMapping("masterWithdrawView")
    @ApiOperation("主持人提现页金额回显")
    public BaseResult<CanWithdrawVo> masterWithdrawView(){
        return new BaseResult<CanWithdrawVo>().ok(masterService.masterWithdrawView());
    }

    @PostMapping("masterWithdraw")
    @ApiOperation("主持人申请提现")
    public BaseResult<String> masterWithdraw(@Validated @RequestBody SaveWithdrawParams params){
        boolean result = masterService.masterWithdraw(params);
        if(result){
            return new BaseResult<String>().success("申请提现成功");
        }
        return new BaseResult<String>().error("申请提现失败");
    }

    @GetMapping("masterWithdrawList")
    @ApiOperation("主持人申请提现列表")
    public BaseResult<PageModel<MasterWithdrawListPageVo>> masterWithdrawList(MasterWithdrawSearchParams params){
        return new BaseResult<PageModel<MasterWithdrawListPageVo>>().ok(masterService.masterWithdrawList(params));
    }

    @GetMapping("masterPoster")
    @ApiOperation("咨询师海报")
    @PassToken
    public BaseResult<QrCodeVo> masterPoster(@RequestParam("masterId") String masterId){
        return new BaseResult<QrCodeVo>().ok(masterService.groupPoster(masterId));
    }
}
